package com.ml.mall.service.mail.impl;

import com.ml.mall.bean.vo.OrderDetailArrVO;
import com.ml.mall.model.MailMessage;
import com.ml.mall.model.mail.admin.MailTempleteForAdmin;
import com.ml.mall.model.mail.general.MailTemplateGeneral;
import com.ml.mall.model.mail.general.MailTemplateVerifyCode;
import com.ml.mall.model.mail.partner.MailTempleteForPartner;
import com.ml.mall.model.mail.purchaser.MailTemplateForPurchaser;
import com.ml.mall.model.mail.supplier.MailTemplateForSupplier;
import com.ml.mall.service.mail.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.List;
import java.util.Properties;

/**
 * @description 邮件发送实现类
 * @author pl
 * @date 2021/7/14
 * @version 1.0
*/
@Service
//@Component
//@PropertySource("classpath:application-test.yml")
//@ConfigurationProperties(prefix = "mail")
public class MailServiceImpl implements MailService {
//    @Value("${mail.is_auth:true}")
    private String isAuth="true";
//    @Value("${mail.mail_port:587}")
    private String port="587";
//    @Value("${mail.smtp_host:smtp.qq.com}")
    private String smtpHost="smtp.qq.com";
//    @Value("${mail.info_mail_user:order@myjombeli.com}")
    private String infoMailUser="2297207040@qq.com";
//    @Value("${mail.order_mail_user:order@myjombeli.com}")
    private String orderMailUser="order@myjombeli.com";
//    @Value("${mail.receive_mail_admin:nexus0928@163.com}")
    private String receiveMailAdmin="nexus0928@163.com";
//    @Value("${mail.mail_pwd:pxmjlybfcpcxecdg}")
    private String mailPwd="pxmjlybfcpcxecdg";

    @Autowired
    private MailTempleteForAdmin mailTempleteForAdmin;

    @Autowired
    private MailTempleteForPartner mailTempleteForPartner;

    @Autowired
    private MailTemplateGeneral mailTemplateGeneral;

    @Autowired
    private MailTemplateVerifyCode mailTemplateVerifyCode;

    @Autowired
    private MailTemplateForPurchaser mailTemplateForPurchaser;

    @Autowired
    private MailTemplateForSupplier mailTemplateForSupplier;

    /**
     * 卖家注册邮件给管理员
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForSupplierRegistration(String recipientEmail){
        MailMessage mailMessage = mailTempleteForAdmin.getSupplierRegistration(recipientEmail);
        return mailMessage;
    }

    /**
     * 买家注册邮件给管理员
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForPurchaserRegistration(String recipientEmail) {
        MailMessage mailMessage = mailTempleteForAdmin.getPurchaserRegistration(recipientEmail);
        return mailMessage;
    }

    /**
     * 合伙人加入邮件给管理员
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForPartnerOnBoard(String recipientEmail) {
        MailMessage mailMessage = mailTempleteForAdmin.getPartnerOnBoard(recipientEmail);
        return mailMessage;
    }

    /**
     * 生成新订单邮件给管理员
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForNewPurchaseOrder(String recipientEmail) {
        MailMessage mailMessage = mailTempleteForAdmin.getNewPurchaseOrder(recipientEmail);
        return mailMessage;
    }

    /**
     * 供应商接收采购订单邮件给管理员
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForSupplierAcceptPurchaseOrder(String recipientEmail) {
        MailMessage mailMessage = mailTempleteForAdmin.getSupplierProcessPurchaseOrder("");
        return mailMessage;
    }

    /**
     * 供应商拒绝采购订单邮件给管理员
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForSupplierRejectPurchaseOrder(String recipientEmail) {
        MailMessage mailMessage = mailTempleteForAdmin.getSupplierRejectPurchaseOrder("");
        return mailMessage;
    }

    /**
     * 公司额度更改邮件给管理员
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForCompanyCreditModified(String recipientEmail) {
        MailMessage mailMessage = mailTempleteForAdmin.getCompanyCreditModified(recipientEmail);
        return mailMessage;
    }

    /**
     * 上传新产品邮件给管理员
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForNewProductUploaded(String recipientEmail) {
        MailMessage mailMessage = mailTempleteForAdmin.getNewProductUploaded(recipientEmail);
        return mailMessage;
    }

    /**
     * 合伙人发起注册邀请邮件给卖家
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForRegistrationInvitationFromPartner(String recipientEmail) {
        MailMessage mailMessage = mailTemplateForSupplier.getRegistrationInvitationFromPartner(recipientEmail);
        return mailMessage;
    }

    /**
     * 卖家成功注册通知邮件：通用
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForRegisterSuccessfulEmail(String recipientEmail) {
        MailMessage mailMessage = mailTemplateForSupplier.getRegisterSuccessfulEmail(recipientEmail);
        return mailMessage;
    }

    /**
     * 账号被批准邮件给卖家
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForAccountApproval(String recipientEmail) {
        MailMessage mailMessage = mailTemplateForSupplier.getAccountApproval(recipientEmail);
        return mailMessage;
    }


    /**
     * 低库存量通知，系统发送 “低库存量通知” 邮件给卖家
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForLowStockNotification(String recipientEmail) {
        MailMessage mailMessage = mailTemplateForSupplier.getLowStockNotification(recipientEmail);
        return mailMessage;
    }

    /**
     * 零库存通知，系统发送 “零库存通知” 邮件给卖家
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForOutOfStockNotification(String recipientEmail) {
        MailMessage mailMessage = mailTemplateForSupplier.getOutOfStockNotification(recipientEmail);
        return mailMessage;
    }

    /**
     * 增加额度通知，系统发送 “增加额度通知” 邮件给卖家
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForCreditNotification(String recipientEmail) {
        MailMessage mailMessage = mailTemplateForSupplier.getCreditNotification(recipientEmail);
        return mailMessage;
    }


    /**
     * 暂停公司通知，卖家通过系统发送 “暂停公司通知” 邮件给买家
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForCompanySuspended(String recipientEmail) {
        MailMessage mailMessage = mailTemplateForSupplier.getCompanySuspended(recipientEmail);
        return mailMessage;
    }


    @Override
    public MailMessage sendForCompanyRejected(String recipientEmail) {
        MailMessage mailMessage = mailTemplateForSupplier.getCompanyRejected(recipientEmail);
        return mailMessage;
    }

    /**
     * 订单支付通知，系统发送 “订单支付通知” 邮件给买家
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForPurchaseOrderPaymentMade(String recipientEmail) {
        MailMessage mailMessage = mailTemplateForPurchaser.getPurchaseOrderPaymentMade(recipientEmail);
        return mailMessage;
    }

    /**
     * 卖家更新订单状态，系统发送 “卖家更新订单状态” 邮件给买家
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForDeliveryScheduleUpdatedByEachSupplier(String recipientEmail) {
        MailMessage mailMessage = mailTemplateForPurchaser.getDeliveryScheduleUpdatedByEachSupplier(recipientEmail);
        return mailMessage;
    }

    /**
     * 订单完成，系统发送 “订单完成” 邮件给买家
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForPurchaseOrderCompleted(String recipientEmail) {
        MailMessage mailMessage = mailTemplateForPurchaser.getPurchaseOrderCompleted(recipientEmail);
        return mailMessage;
    }

    /**
     * 优惠卷过期通知，系统发送 “优惠卷过期通知” 邮件给买家
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForCouponExpirationReminder(String recipientEmail) {
        MailMessage mailMessage = mailTemplateForPurchaser.getCouponExpirationReminder(recipientEmail);
        return mailMessage;
    }


    /**
     * 用户邀请链接，通用
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForUserInvitationForCompany(String recipientEmail) {
        MailMessage mailMessage = mailTemplateForPurchaser.getUserInvitation(recipientEmail);
        return mailMessage;
    }


    /**
     * 合伙人邀请链接，系统发送 “合伙人邀请链接” 邮件给合作商
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForPartnerInvitationLink(String recipientEmail) {
        MailMessage mailMessage = mailTempleteForPartner.getPartnerInvitationLink(recipientEmail);
        return mailMessage;
    }

    /**
     * 邀请的卖家账号被批准，系统发送 “邀请的卖家账号被批准” 邮件给合作商
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForInvitedSupplierApproved(String recipientEmail) {
        MailMessage mailMessage = mailTempleteForPartner.getInvitedSupplierApproved(recipientEmail);
        return mailMessage;
    }

    /**
     * 重置密码，系统发送 “重置密码” 邮件给买家
     * @param recipientEmail
     */
    @Override
    public MailMessage sendForResetPassword(String recipientEmail) {
        MailMessage mailMessage = mailTemplateGeneral.getResetPassword(recipientEmail);
        return mailMessage;
    }

    @Override
    public MailMessage sendForProductRejected(String recipientEmail) {
        return mailTemplateForSupplier.getProductRejectedEmail(recipientEmail);
    }

    @Override
    public MailMessage sendVerifyCode(String verifyCode,String to) {
        MailMessage mailMessage = mailTemplateVerifyCode.verifyCode(verifyCode,to);
        return mailMessage;
    }
    @Override
    public MailMessage sendForCompanyChangeSubType(String to) {
        MailMessage mailMessage=mailTemplateForSupplier.getSubjectChangeEmail(to);
        return mailMessage;
    }

    /**
     * 正式发送邮件
     * @param mailMessage
     */
    @Override
    public void sendMail(MailMessage mailMessage){
        //1.配置环境
        Properties props = new Properties();
        //2.将yml文件配置导入环境中
        props.setProperty("is-auth",isAuth);
        props.setProperty("port",port);
        props.setProperty("smtp-host",smtpHost);
        props.setProperty("info-mail-user",infoMailUser);//系统邮箱
        props.setProperty("order-mail-user",orderMailUser);
        props.setProperty("receive-mail-admin",mailMessage.getReceiverEmail());
        props.setProperty("mail-pwd",mailPwd);
        //验证邮箱
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                PasswordAuthentication pwdAuth = new PasswordAuthentication(props.getProperty("info-mail-user"),props.getProperty("mail-pwd"));
                return pwdAuth;
            }
        };
        //创建邮件的会话
        Session mailSession = Session.getInstance(props,authenticator);
        //由会话创建MimeMessage
        MimeMessage mimeMessage = new MimeMessage(mailSession);
        try {
            //设置发件人
            Address from = new InternetAddress(props.getProperty("info-mail-user"));
            mimeMessage.setFrom(from);
            //设置收件人,setRecipient()只能发给一个人，但是addRecipients()可以同时发给多个人
            Address to = new InternetAddress(mailMessage.getReceiverEmail());
            mimeMessage.addRecipient(Message.RecipientType.TO,to);
            //设置标题
            mimeMessage.setSubject(mailMessage.getSubject(),"UTF-8");
            //设置文本内容
            mimeMessage.setContent(mailMessage.getContent(),"text/html;charset=UTF-8");
            Transport.send(mimeMessage);
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }




}
